package com.decent.manager.system.dao.provider;

import cn.hutool.core.util.StrUtil;
import com.decent.common.entity.manager.SystemUser;
import com.decent.manager.system.dto.SystemUserDTO;

import java.util.HashMap;
import java.util.Map;
import java.util.Objects;

/**
 * 系统用户sql管理
 *
 * @author wangyan
 **/
public class SystemUserDaoProvider {
    /**
     * 根据用户id更新用户
     *
     * @param params 用户数据
     */
    @SuppressWarnings("unused")
    public String updateUserById(Map<String, SystemUser> params) {
        SystemUser user = params.get("user");
        StringBuilder sql = new StringBuilder(64);
        sql.append(" update system_user set ");
        StringBuilder updateSetBuilder = new StringBuilder(32);
        if (StrUtil.isNotBlank(user.getUserAccount())) {
            updateSetBuilder.append(" user_account = #{user.userAccount} ,");
        }
        if (StrUtil.isNotBlank(user.getLoginPassword())) {
            updateSetBuilder.append(" login_password = #{user.loginPassword} , ");
        }
        if (StrUtil.isNotBlank(user.getUserName())) {
            updateSetBuilder.append(" user_name = #{user.userName} ,");
        }
        if (StrUtil.isNotBlank(user.getMobile())) {
            updateSetBuilder.append(" mobile = #{user.mobile} ,");
        }
        if (StrUtil.isNotBlank(user.getLastLoginIp())) {
            updateSetBuilder.append(" last_login_ip = #{user.lastLoginIp} ,");
        }
        if (Objects.nonNull(user.getLastLoginTime())) {
            updateSetBuilder.append(" last_login_time = #{user.lastLoginTime} ,");
        }
        if (Objects.nonNull(user.getUserType())) {
            updateSetBuilder.append(" user_type = #{user.userType} ,");
        }
        if (Objects.nonNull(user.getUserStatus())) {
            updateSetBuilder.append(" user_status = #{user.userStatus} ,");
        }
        if (Objects.nonNull(user.getUpdateTime())) {
            updateSetBuilder.append(" update_time = #{user.updateTime} ,");
        }
        if (Objects.nonNull(user.getLastModifier())) {
            updateSetBuilder.append(" last_modifier = #{user.lastModifier} ,");
        }
        String updateSet = StrUtil.removeSuffix(StrUtil.trim(updateSetBuilder.toString()), ",");
        sql.append(updateSet).append(" where id = #{user.id} ");
        return sql.toString();
    }

    /**
     * 分页查询员工列表
     *
     * @param param 查询条件
     * @return sql
     */
    @SuppressWarnings("unused")
    public String queryPage(HashMap<String, SystemUserDTO> param) {
        StringBuilder sql = new StringBuilder(127);
        sql.append("select u.user_account, u.user_name, u.mobile, u.user_type, u.user_status, u.remark, " +
                "u.create_time, u.creator, u.update_time, u.last_modifier, r.role_name ,ur.role_id from system_user u, " +
                "system_role r, system_user_role ur where u.id = ur.user_id and ur.role_id = r.id  ");
        SystemUserDTO user = param.get("user");
        if (StrUtil.isNotBlank(user.getUserAccount())) {
            sql.append(" and u.user_account = #{user.userAccount} ");
        }
        if (StrUtil.isNotBlank(user.getUserName())) {
            sql.append(" and u.user_name = #{user.userName} ");
        }
        if (StrUtil.isNotBlank(user.getMobile())) {
            sql.append(" and u.mobile = #{user.mobile} ");
        }
        if (Objects.nonNull(user.getUserStatus())){
            sql.append(" and u.user_status = #{user.userStatus} ");
        }
        if (Objects.nonNull(user.getRoleId())){
            sql.append(" and r.id = #{user.roleId} ");
        }
        sql.append(" order by u.id");
        return sql.toString();
    }

    /**
     * 创建员工
     *
     * @param param 新增条件
     * @return sql
     */
    @SuppressWarnings("unused")
    public String createUser(HashMap<String, SystemUserDTO> param) {
        StringBuilder sql = new StringBuilder(127);
        StringBuilder values = new StringBuilder(127);
        sql.append("insert into system_user (user_account, login_password, user_name ");
        values.append(" values (#{user.userAccount}, #{user.loginPassword}, #{user.userName} ");
        SystemUserDTO user = param.get("user");
        if (StrUtil.isNotBlank(user.getMobile())) {
            sql.append(", mobile ");
            values.append(", #{user.mobile} ");
        }
        sql.append(", user_type, user_status, create_time, creator, update_time, last_modifier) ");
        values.append(", #{user.userType}, #{user.userStatus}, now(), #{user.creator}, now(), #{user.creator}) ");
        return sql.append(values).toString();
    }
}
